import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.text.Font;
import Top1.DerivationTree2;


public class TreeDisplayer 
{
	
	
	/*************************************************************************************************************/
	/** Title: MakeNode																				
	/** Description: Creates a new textField in the right position in the GUI and subsequently, a new pattern node 			
	/*************************************************************************************************************/
	
	public static Label MakeNode (final double x , final double y, final Pane canvas, DerivationTree2 tree)
	{
		Label node = new Label ( tree.getDerivedFact().toString() );
    	node.setLayoutX(x);
    	node.setLayoutY(y);
    	node.setMaxSize(100, 50);
    	node.setFont(new Font(10));
    	node.setAlignment(Pos.CENTER);
    	
    	canvas.getChildren().add(node);
    	return node;
	}
	
	
	
	/*************************************************************************************************************/
	/** Title: AddLine																				
	/** Description: Creates a line between two nodes. If trans -> dashed line. If not trans -> regular line 			
	/*************************************************************************************************************/
	/*
	public static void AddLine (Label childfield, final Pane root, DerivationTree2 tree)
	{
		root.getChildren().removeAll(childfield.getAttachedLines());
		cm.getAttachedLines().clear();
		
		for (DerivationTree2 child : tree.getChildren()) 
		{
			final Line line = new Line();
			line.setStartX(cm.getParent().getLayoutX() + 50);
		    line.setStartY(cm.getParent().getLayoutY() + 25);
		    line.setEndX(child.getParent().getLayoutX() + 50);
		    line.setEndY(child.getParent().getLayoutY());
		    
		    cm.addAttachedLine(line);
		    root.getChildren().add(line);
		}
	}
	
	*/
	
	/*************************************************************************************************************/
	/** Title: getPattern																				
	/** Description: Parses the user generated pattern and creates a Pattern object. 			
	/*************************************************************************************************************/
	
	public static void getTree (DerivationTree2 tree, double x , double y, final Pane canvas)
	{
		Label root = MakeNode(x, y, canvas, tree);
		drawSubTree(tree, root, canvas);
	}

	
	
	/*************************************************************************************************************/
	/** Title: drawSubTree																				
	/** Description: The recursive method to help getPattern() parse the GUI pattern.			
	/*************************************************************************************************************/
	
	private static void drawSubTree (DerivationTree2 tree, Label node, final Pane canvas)
	{
		int offset = 1 - tree.getChildren().size();
		for (DerivationTree2 child : tree.getChildren()) 
		{
			Label labelChild = MakeNode(node.getLayoutX() + 70 * offset, node.getLayoutY() + 50, canvas, child);
			drawSubTree(child, labelChild, canvas);
			offset = offset + 2;
		}
	}
}
